package com.xq.shop.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.xq.shop.dao.ICartGoodDAO;
import com.xq.shop.po.CartGood;
import com.xq.shop.util.HibernateUtil;

@Repository
@SuppressWarnings("unchecked")
public class CartGoodDAO extends BaseDAO<CartGood> implements ICartGoodDAO {

	@Override
	public List<CartGood> getByUID(Long userId) {
		return HibernateUtil.getTemplate().find(" from CartGood where user_id=? ", userId);
	}

	@Override
	public CartGood get(Long userId, Long productId) {
		String hql = "from CartGood c where c.user_id=? and product_id=?";
		List<CartGood> list = HibernateUtil.getTemplate().find(hql, new Long[]{userId, productId});
		if(list != null && list.size() > 0){
			return list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public void delete(Long id, Long userId) {
		String hql = "delete CartGood where id = :id and user_id = :userId";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", id);
		param.put("userId", userId);
		HibernateUtil.execute(hql, param);
	}
	
	public void delete(Long userId, Long[] productId){
		String hql = "delete CartGood where user_id = :userId and product_id in (:productId)";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("productId", productId);
		param.put("userId", userId);
		HibernateUtil.execute(hql, param);
	}
}
